﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

namespace ShoppingApp
{
    public partial class Shops : System.Web.UI.Page
    {
        
        /// <summary>
        /// Creates connection string used to connect to ShoppingAppDb
        /// </summary>
        /// <returns>String</returns>
        /// <remarks>This connectionString should be in Web.config file</remarks>
        private string BuildConnectionString()
        {
            SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
            builder.DataSource = @".\SQLEXPRESS";
            builder.InitialCatalog = "ShoppingAppDb";
            builder.UserID = "AspNetUser";
            builder.Password = "password";

            
            return builder.ConnectionString;
        }


        protected void Page_Load(object sender, EventArgs e)
        {
            LoadButton_Click(null, null);

            //At first read of page the date is read. When postback occurs data is taken from ViewState
            if (!Page.IsPostBack)
            {
                //If data have been loaded into gridview, show it so user don't have to click the button all the time
                LoadButton_Click(null, null);
            }
        }

        protected void LoadButton_Click(object sender, EventArgs e)
        {
            
            //SqlDataSource1.SelectCommandType = SqlDataSourceCommandType.StoredProcedure;
            SqlDataSource1.ConnectionString = BuildConnectionString();
            SqlDataSource1.SelectCommand = "SELECT Name,ChainName FROM dbo.Shops";
            GridView1.DataBind();

            //GridView1.DataBind();
        }

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            //CommandName determines which group of controls (buttons in which column) has been pressed
            //It differentiates buttons from other buttons - user chooses this name
            if (e.CommandName == "Add")
            {
                //Convert the row index stored in CommandArgument property to integer
                int index = Convert.ToInt32(e.CommandArgument);

                GridViewRow selectedRow = GridView1.Rows[index];
                TableCell shopName = selectedRow.Cells[1];
                string name = shopName.Text;

                System.Diagnostics.Debug.WriteLine(name);

            }
        }

        protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Header)
            {
                int sortIndexColumn = GetSortColumnIndex();

                if (sortIndexColumn > 0)
                {
                    AddSortImage(sortIndexColumn, e.Row);
                }
            }            
        }

        protected int GetSortColumnIndex()
        {
            foreach (DataControlField field in GridView1.Columns)
            {
                if (field.SortExpression == GridView1.SortExpression)
                {
                    return GridView1.Columns.IndexOf(field);
                }
            }

            return -1;
        }

        protected void AddSortImage(int columnIndex, GridViewRow headerRow)
        {
            Image sortImage = new Image();
            sortImage.Width = Unit.Pixel(50);
            sortImage.ImageAlign = ImageAlign.Right;

            if (GridView1.SortDirection == SortDirection.Ascending)
            {
                sortImage.ImageUrl = @"Images\triangle.jpg";
            //    sortImage.Width = Unit.Pixel(50);
                
            }
            else
            {
                sortImage.ImageUrl = @"Images\triangle_desc.jpg";
                //headerRow.Cells[columnIndex].Controls.Add(sortImage);
                //headerRow.Cells[columnIndex].Controls.Remove(sortImage);
            }
            headerRow.Cells[columnIndex].Controls.Add(sortImage);
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                if (e.Row.RowIndex == 0)
                {
                    //TableCell cell = new TableCell();
                    //cell.Text = "Pierwszy";
                    //HyperLinkField hlf = new HyperLinkField();
                    //hlf.NavigateUrl = "Details.aspx";
                    ////e.Row.Cells[0].Controls.Add();
                    //e.Row.Cells[0].Text = "Pierwszy";
                    //GridViewRow row = new GridViewRow(0, 0, DataControlRowType.DataRow, DataControlRowState.Normal);
                    //row.Cells.Add(cell);
                    //Table tbl = (Table)e.Row.Parent;
                    ////GridView1.Controls[0].Controls.AddAt(GridView1.Controls[0].Controls.Count - 1, row);
                    //tbl.Rows.AddAt(1, row);


                    TableCell cell = new TableCell();
                    //cell.Width = Unit.Pixel(200);
                    //Button btn = new Button();
                    //btn.Text = "Desc";
                    //btn.Click += new EventHandler(btn_Click);
                    //cell.Controls.AddAt(0, btn);
                    //Button btn2 = new Button();
                    //btn2.Text = "Asc";
                    //cell.Controls.AddAt(1, btn2);

                    ImageButton imgBtn = new ImageButton();
                    imgBtn.ID = "NameAscImageButton";
                    imgBtn.ImageUrl = @"/Images/triangle.jpg";
                    imgBtn.Width = Unit.Pixel(50);
                    imgBtn.Click += new ImageClickEventHandler(imgBtn_Click);
                    cell.Controls.Add(imgBtn);

                    ImageButton imgBtn2 = new ImageButton();
                    imgBtn2.ID = "NameDescImageButton";
                    imgBtn2.ImageUrl = @"/Images/triangle_desc.jpg";
                    imgBtn2.Width = Unit.Pixel(50);
                    imgBtn2.Click += new ImageClickEventHandler(imgBtn2_Click);
                    cell.Controls.Add(imgBtn2);


                    GridViewRow row = new GridViewRow(0, 0, DataControlRowType.DataRow, DataControlRowState.Normal);
                    for (int i = 0; i < e.Row.Cells.Count; i++)
                    {
                        //Column named "Name"
                        if (i == 1)
                        {
                            row.Cells.Add(cell);
                        }
                        else
                        {
                            row.Cells.Add(new TableCell());
                        }
                    }

                    //row.Cells.Add(cell);
                    Table tbl = (Table)e.Row.Parent;
                    //tbl.Rows.Add(row);
                    tbl.Rows.AddAt(1, row);

                }

            } 
        }

        void imgBtn_Click(object sender, ImageClickEventArgs e)
        {
            GridView1.Sort("Name", SortDirection.Ascending);
        }

        void imgBtn2_Click(object sender, ImageClickEventArgs e)
        {
            GridView1.Sort("Name", SortDirection.Descending);
        }

        void btn_Click(object sender, EventArgs e)
        {
            
            GridView1.Sort("Name" , SortDirection.Descending);
            //Response.Write("Button clicked");
            
            //throw new NotImplementedException();
        }
    }
}